module.exports = function(window, document) {
	var apivm = require('../../common/avm.min')(window, document);
	var avm = apivm;
	var api = apivm.api;
	var Component = apivm.Component;
	var $ = apivm.$;
	(function() {
	/*
	 * APICloud JavaScript Library
	 * Copyright (c) 2014 apicloud.com
	 */
	var $kn = {
		byId: function byId(id) {
			return document.getElementById(id);
		},
		ajax: function ajax(p, callback) {
			var _this = this;
			var param = p;
			if (!param.headers) {
				param.headers = {};
			}
			param.headers["x-apicloud-mcm-key"] = "cZKzX7DabDmYyfez";
			if (param.data && param.data.body) {
				param.headers["Content-Type"] = "application/json; charset=utf-8";
			}
			if (param.url) {
				var baseUrl = "https://a8888888888888-pd.apicloud-saas.com/api/";
				param.url = baseUrl + param.url;
			}
			api.ajax(param, function(ret, err) {
				if (callback) callback(ret, err);
				if (ret) {
					var status = ret.status;
					if (status && status == 4001) {
						var didShowLogoutAlert = api.getGlobalData({
							key: "didShowLogoutAlert"
						});

						if (!didShowLogoutAlert) {
							api.setGlobalData({
								key: "didShowLogoutAlert",
								value: true
							});

							_this.setUserInfo("");
							api.alert(
								{
									msg: "登录已失效，请重新登录"
								},
								function() {
									api.setGlobalData({
										key: "didShowLogoutAlert",
										value: false
									});

									api.closeToWin({
										name: "root"
									});
								}
							);
						}
					}
				}
			});
		},
		getUserInfo: function getUserInfo() {
			var value = api.getPrefs({
				key: "userInfo",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setUserInfo: function setUserInfo(userInfo) {
			api.setPrefs({
				key: "userInfo",
				value: userInfo
			});
		},
		getCurrentCityInfo: function getCurrentCityInfo() {
			var value = api.getPrefs({
				key: "currentCity",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setCurrentCityInfo: function setCurrentCityInfo(cityInfo) {
			api.setPrefs({
				key: "currentCity",
				value: cityInfo
			});
		},
		getWareTypeList: function getWareTypeList() {
			var value = api.readFile({
				sync: true,
				path: "fs://WareTypeList"
			});

			return value ? JSON.parse(value) : "";
		},
		setWareTypeList: function setWareTypeList(list) {
			api.writeFile({
				path: "fs://WareTypeList",
				data: JSON.stringify(list)
			});
		},
		fitRichText: function fitRichText(richtext, width) {
			var str = '<img style="max-width:' + width + 'px;"';
			var result = richtext.replace(/\<img/gi, str);
			return result;
		}
	};

	var StatusBarView = /*@__PURE__*/ (function(Component) {
		function StatusBarView(props) {
			Component.call(this, props);
			this.compute = {
				statusBarHeight: function() {
					if (api.safeArea) {
						return api.safeArea.top;
					} else {
						var res = wx.getSystemInfoSync();
						return res.statusBarHeight;
					}
				}
			};
		}

		if (Component) StatusBarView.__proto__ = Component;
		StatusBarView.prototype = Object.create(Component && Component.prototype);
		StatusBarView.prototype.constructor = StatusBarView;
		StatusBarView.prototype.render = function() {
			return apivm.h("view", {style: "height:" + this.statusBarHeight + "px;"});
		};

		return StatusBarView;
	})(Component);
	apivm.define("status-bar-view", StatusBarView);

	var Navigationbar = /*@__PURE__*/ (function(Component) {
		function Navigationbar(props) {
			Component.call(this, props);
		}

		if (Component) Navigationbar.__proto__ = Component;
		Navigationbar.prototype = Object.create(Component && Component.prototype);
		Navigationbar.prototype.constructor = Navigationbar;
		Navigationbar.prototype.onLeftButton = function(e) {
			api.closeWin();
		};
		Navigationbar.prototype.render = function() {
			return apivm.h(
				"view",
				{class: "nav-container"},
				apivm.h("status-bar-view", null),
				apivm.h(
					"view",
					{class: "nav-header"},
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-left-button",
							onclick: this.props.onLeftButton
								? this.props.onLeftButton
								: this.onLeftButton
						},
						apivm.h("image", {
							width: this.props.leftButtonWidth ? this.props.leftButtonWidth : 11,
							src: this.props.leftButtonIcon
								? this.props.leftButtonIcon
								: "../../image/back.png",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.leftButtonText)
					),
					apivm.h("text", {class: "nav-header-title"}, this.props.title),
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-right-button",
							onclick: this.props.onRightButton
						},
						apivm.h("image", {
							width: this.props.rightButtonWidth ? this.props.rightButtonWidth : 0,
							src: this.props.rightButtonIcon ? this.props.rightButtonIcon : "",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.rightButtonText)
					)
				)
			);
		};

		return Navigationbar;
	})(Component);
	apivm.define("navigationBar", Navigationbar);

	var Shoppingcart = /*@__PURE__*/ (function(Component) {
		function Shoppingcart(props) {
			Component.call(this, props);
			this.data = {
				haveData: false,
				dataList: [],
				count: 0,
				amount: 0
			};
		}

		if (Component) Shoppingcart.__proto__ = Component;
		Shoppingcart.prototype = Object.create(Component && Component.prototype);
		Shoppingcart.prototype.constructor = Shoppingcart;
		Shoppingcart.prototype.apiready = function() {
			var that = this;
			// 监听购物车更新事件(自定义事件)，同步更新列表展示中选定商品的数量
			api.addEventListener(
				{
					name: "onGetCartInfo"
				},
				function(ret, err) {
					var data = ret.value;
					var wareList = data.wareList;
					that.data.haveData = wareList.length > 0 ? true : false;
					that.data.dataList = wareList;
					that.fnGetShoppingCartAmount(wareList);
				}
			);
			api.addEventListener(
				{
					name: "navitembtn"
				},
				function(ret) {
					if (ret.type == "left") {
						api.closeWin();
					}
				}
			);
			this.getCartInfo();
		};
		Shoppingcart.prototype.getCartInfo = function() {
			var that = this;
			api.showProgress({
				title: "",
				text: ""
			});

			$kn.ajax(
				{
					url: "carts/getCartInfo"
				},
				function(ret, err) {
					if (ret && ret.data) {
						var data = ret.data;
						var wareList = data.wareList;
						if (wareList.length > 0) {
							that.data.haveData = true;
							that.data.dataList = wareList;
							that.fnGetShoppingCartAmount(wareList);

							var selectAll = true;
							for (var i = 0; i < wareList.length; i++) {
								if (!wareList[i].selected) {
									selectAll = false;
									break;
								}
							}
							$kn.byId("selectAll").checked = selectAll;
						}
					}
					api.hideProgress();
				}
			);
		};
		Shoppingcart.prototype.fnGetShoppingCartAmount = function(data_) {
			var amountValue = 0;
			var countValue = 0;
			for (var i = 0; i < data_.length; i++) {
				if (!data_[i].selected) {
					continue;
				}
				countValue += data_[i].wareCount;
				amountValue += data_[i].price * data_[i].wareCount;
			}
			amountValue = Math.round(amountValue * 10) / 10;
			this.data.amount = amountValue;
			this.data.count = countValue;
		};
		Shoppingcart.prototype.fnSelectedStateChanged = function(e) {
			var index = e.currentTarget.dataset.index;
			var item = this.data.dataList[index];
			item.selected = e.detail.checked;
			this.fnGetShoppingCartAmount(this.data.dataList);

			// 发送购物车更新自定义事件
			api.sendEvent({
				name: "updateShoppingCart",
				extra: {
					wareId: item.id,
					wareCount: item.wareCount,
					selected: item.selected
				}
			});
		};
		Shoppingcart.prototype.fnOpenDetailWin = function(e) {
			var id = e.currentTarget.dataset.id;
			var wareCount = e.currentTarget.dataset.wareCount;
			api.openWin({
				name: "ware" + id,
				url: "../ware/ware.stml",
				pageParam: {
					wareId: id,
					wareCount: wareCount
				},

				animation: {
					type: "fade"
				}
			});
		};
		Shoppingcart.prototype.fnMinus = function(e) {
			var index = e.currentTarget.dataset.index;
			var item = this.data.dataList[index];
			if (item.wareCount > 0) {
				item.wareCount--;
				var listView = document.getElementById("listView");
				if (item.wareCount > 0);
				else {
					this.data.dataList.splice(index, 1);
				}

				// 发送购物车更新自定义事件，传递商品Id和商品数量
				api.sendEvent({
					name: "updateShoppingCart",
					extra: {
						wareId: item.id,
						wareCount: item.wareCount,
						selected: item.selected
					}
				});
			}
			this.fnGetShoppingCartAmount(this.data.dataList);
			if (this.data.dataList.length == 0) {
				this.data.haveData = false;
			}
		};
		Shoppingcart.prototype.fnAdd = function(e) {
			var index = e.currentTarget.dataset.index;
			var item = this.data.dataList[index];
			item.wareCount++;

			// 发送购物车更新自定义事件，传递商品Id和商品数量
			api.sendEvent({
				name: "updateShoppingCart",
				extra: {
					wareId: item.id,
					wareCount: item.wareCount,
					selected: item.selected
				}
			});

			this.fnGetShoppingCartAmount(this.data.dataList);
		};
		Shoppingcart.prototype.fnSelectAll = function(e) {
			if (!this.data.dataList) {
				return;
			}
			for (var i = 0; i < this.data.dataList.length; i++) {
				this.data.dataList[i].selected = e.detail.checked;
			}
			this.fnGetShoppingCartAmount(this.data.dataList);
			$kn.ajax(
				{
					url: "carts/selectAll",
					method: "post",
					data: {
						body: {
							selectAll: e.detail.checked
						}
					}
				},

				function(ret, err) {}
			);
		};
		Shoppingcart.prototype.fnCloseWin = function() {
			api.closeWin();
		};
		Shoppingcart.prototype.fnOpenOrderWin = function() {
			if (this.data.count == 0) {
				api.toast({
					msg: "未选择商品"
				});

				return;
			}

			var dataList = [];
			for (var i = 0; i < this.data.dataList.length; i++) {
				if (this.data.dataList[i].selected) {
					dataList.push(this.data.dataList[i]);
				}
			}
			api.openWin({
				name: "order",
				url: "../order/order.stml",
				pageParam: {
					dataList: dataList
				}
			});
		};
		Shoppingcart.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "main"},
				apivm.h("navigationBar", {title: "购物车"}),
				apivm.h(
					"view",
					{class: "content"},
					this.data.haveData
						? apivm.h(
								"scroll-view",
								{"scroll-y": "true", class: "listView"},
								Object.entries(this.data.dataList).map(function(ref) {
									var index$1 = ref[0];
									var item$1 = ref[1];
									return apivm.h(
										"view",
										{class: "cell"},
										apivm.h(
											"view",
											{
												"data-id": item$1.id,
												"data-wareCount": item$1.wareCount,
												class: "container",
												onclick: this$1.fnOpenDetailWin
											},
											apivm.h(
												"label",
												null,
												apivm.h("checkbox", {
													checked: item$1.selected,
													"data-index": index$1,
													class: "checkbox",
													icon: "../../image/order_off.png",
													selectedIcon: "../../image/order_on.png",
													onchange: this$1.fnSelectedStateChanged
												})
											),
											apivm.h("image", {
												class: "thumbnail",
												src: item$1.thumbnail,
												placeholder: "../../image/default_square.png"
											}),
											apivm.h(
												"view",
												{class: "info"},
												apivm.h(
													"text",
													{class: "info-name"},
													item$1.name + " " + (item$1.unit || "")
												),
												apivm.h("text", {class: "info-description"}, item$1.description),
												apivm.h("text", {class: "info-price"}, "￥" + item$1.price),
												apivm.h(
													"text",
													{class: "info-origin-price"},
													"￥" + item$1.originPrice
												)
											),
											apivm.h(
												"view",
												{class: "control"},
												apivm.h("image", {
													class: item$1.wareCount > 0 ? "minus" : "none",
													"data-index": index$1,
													src: "../../image/minus.png",
													onclick: this$1.fnMinus
												}),
												apivm.h(
													"text",
													{class: item$1.wareCount > 0 ? "count" : "none"},
													item$1.wareCount
												),
												apivm.h("image", {
													class: "add",
													"data-index": index$1,
													src: "../../image/add.png",
													onclick: this$1.fnAdd
												})
											)
										)
									);
								})
						  )
						: apivm.h(
								"view",
								{id: "empty", class: "empty"},
								apivm.h("image", {
									class: "empty-icon",
									src: "../../image/shoppingcart_empty.png"
								}),
								apivm.h("view", {
									class: "closeBtn",
									onclick: function(event) {
										if (this$1.fnCloseWin) {
											this$1.fnCloseWin();
										} else {
											fnCloseWin();
										}
									}
								})
						  )
				),
				apivm.h(
					"view",
					{class: "footer"},
					apivm.h(
						"label",
						{class: "left"},
						apivm.h("checkbox", {
							id: "selectAll",
							class: "checkbox",
							icon: "../../image/order_off.png",
							selectedIcon: "../../image/order_on.png",
							onchange: this.fnSelectAll
						}),
						apivm.h(
							"view",
							null,
							apivm.h("text", {style: "color:#000;"}, "全选"),
							apivm.h("text", {class: "bottomInfo"}, "已选" + this.data.count + "件")
						)
					),
					apivm.h(
						"view",
						{class: "right"},
						apivm.h(
							"view",
							{class: "right-info"},
							apivm.h(
								"view",
								{class: "right-info-top"},
								apivm.h("text", null, "总金额:"),
								apivm.h("text", {class: "amount"}, "￥" + this.data.amount)
							),
							apivm.h("text", {class: "bottomInfo"}, "不含运费")
						),
						apivm.h("image", {
							class: "buy-button",
							src: "../../image/buy.png",
							onclick: function(event) {
								if (this$1.fnOpenOrderWin) {
									this$1.fnOpenOrderWin();
								} else {
									fnOpenOrderWin();
								}
							}
						})
					)
				),
				apivm.h("safe-area", null)
			);
		};

		return Shoppingcart;
	})(Component);
	apivm.define("shoppingcart", Shoppingcart);
	apivm.render(apivm.h("shoppingcart", null), "body");
})();

	window['createApp'] = function() {
		apivm.$_start();
	}
	return avm;
}